package com.lei.android.videoline.VideoLineView;


import android.annotation.SuppressLint;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import static com.lei.android.videoline.VideoLineView.VideoLineBar.SCALE_VALUE_MILLISECOND;


/**
 * 刻度尺时间工具类
 */
public class VideoLineDateUtils {

    /**
     * 获取当前时间毫秒值
     *
     * @return
     */
    public static long currentTimeMillis() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当前时间的起点（00:00:00）
     *
     * @param currentTime
     * @return
     */
    public static long getTodayStart(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当前时间的终点（23:59:59）
     *
     * @param currentTime
     * @return
     */
    public static long getTodayEnd(long currentTime) {
        return getTodayStart(currentTime) + 24 * 60 * 60 * 1000L - 1000;
    }

    public static int getYear(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取当前时间的小时值
     *
     * @param currentTime
     * @return
     */
    public static int getHour(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取当前时间的分钟值
     *
     * @param currentTime
     * @return
     */
    public static int getMinute(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取当前时间的秒值
     *
     * @param currentTime
     * @return
     */
    public static int getSecond(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 获取当前时间的毫秒值
     *
     * @param currentTime
     * @return
     */
    public static int getMillisecond(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.MILLISECOND);
    }

    /**
     * 获取当前时间的秒值+毫秒值
     *
     * @param currentTime
     * @return
     */
    public static int getSecondMillisecond(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.SECOND) * 1000 + calendar.get(Calendar.MILLISECOND);
    }

    /**
     * 获取当前时间的分钟值+秒值+毫秒值
     *
     * @param currentTime
     * @return
     */
    public static int getMinuteSecondMillisecond(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.MINUTE) * 60 * 1000 + calendar.get(Calendar.SECOND) * 1000 + calendar.get(Calendar.MILLISECOND);
    }

    /**
     * 获取当前时间的小时值+分钟值+秒值+毫秒值
     *
     * @param currentTime
     * @return
     */
    public static int getHourMinuteSecondMillisecond(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 + calendar.get(Calendar.MINUTE) * 60 * 1000 + calendar.get(Calendar.SECOND) * 1000 + calendar.get(Calendar.MILLISECOND);
    }

    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateByCurrentTiem(long currentTime) {
        return new SimpleDateFormat("yyyy-MM-dd").format(currentTime);
    }

    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
//    public static String getDateTime(long currentTime) {
//        return getDateTime(currentTime, "yyyy-MM-dd HH:mm:ss");
//    }

//    @SuppressLint("SimpleDateFormat")
//    public static String getDateTime(long currentTime, String format) {
//        return new SimpleDateFormat(format).format(currentTime);
//    }

    /**
     * 获取大刻度尺时间显示（处理误差专用, 误差1分钟）eg:2019-10-19 23:59:59
     *
     * @param currentTime
     * @param format
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getMaxScaleTime(long currentTime, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));

        if (calendar.get(Calendar.MINUTE) > 30) {
            currentTime = currentTime + 30 * 60 * 1000L;
            calendar.setTime(new Date(currentTime));
            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
            calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            currentTime = calendar.getTimeInMillis();
            return long2StringTime(currentTime, format);

        } else if (calendar.get(Calendar.MINUTE) < 30) {
            currentTime = currentTime - 30 * 60 * 1000L;
            calendar.setTime(new Date(currentTime));
            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
            calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            currentTime = calendar.getTimeInMillis();
            return long2StringTime(currentTime, format);
        }

        return long2StringTime(currentTime, format);
    }

    /**
     * 根据当前的秒数计算时间
     *
     * @param currentSecond
     * @return
     */
    public static String getTimeByCurrentSecond(int currentSecond) {
        currentSecond = currentSecond / 60;
        int minute = currentSecond % 60;
        int hour = currentSecond / 60;
        if (hour >= 24) {
            hour = hour % 24;
        }
        return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
    }

    /**
     * 根据当前的秒数计算时间
     *
     * @param currentSecond
     * @return
     */
    public static String getTimeByCurrentHours(int currentSecond) {
        currentSecond = currentSecond * 10;
        currentSecond = currentSecond / 60;
        int minute = currentSecond % 60;
        int hour = currentSecond / 60;
        if (hour >= 24) {
            hour = hour % 24;
        }
        return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
    }

    /**
     * String时间 转毫秒值
     *
     * @param time   时间
     * @param format yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long string2LongTime(String time, String format) {
        return string2LongTime(time, format, TimeZone.getDefault());
    }

    @SuppressLint("SimpleDateFormat")
    public static long string2LongTime(String time, String format, TimeZone timeZone) {
        Calendar calendar = Calendar.getInstance();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            simpleDateFormat.setTimeZone(timeZone);
            Date date = simpleDateFormat.parse(time);
            calendar.setTime(date);
            calendar.setTimeZone(timeZone);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar.getTimeInMillis();
    }

    /**
     * 毫秒值 转 String时间
     *
     * @param timeMillis
     * @param format
     * @return
     */
    public static String long2StringTime(long timeMillis, String format) {
        return long2StringTime(timeMillis, format, TimeZone.getDefault());
    }

    @SuppressLint("SimpleDateFormat")
    public static String long2StringTime(long timeMillis, String format, TimeZone timeZone) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(timeZone);
        return dateFormat.format(timeMillis);
    }

    //=============绘制视频区域，判断时间Begin====================

    /**
     * 根据时间获取刻度位置
     *
     * @param fristScaleTime
     * @param nowTime
     * @return
     */
    public static float getScaleIndexByTime(long fristScaleTime, long nowTime) {
        return (nowTime - fristScaleTime) / (float) SCALE_VALUE_MILLISECOND;
    }

    /**
     * 根据刻度位置，获取刻度所在的时间
     *
     * @param fristScaleTime
     * @param scaleIndex
     * @return
     */
    public static long getScaleValueByScaleIndex(long fristScaleTime, long scaleIndex) {
        return fristScaleTime + scaleIndex * SCALE_VALUE_MILLISECOND;
    }

    /**
     * 判断时间是否在item内
     *
     * @param nowTime
     * @param itemBeginTime
     * @param itemEndTime
     * @return
     */
    public static boolean isInnerItemArea(long nowTime, long itemBeginTime, long itemEndTime) {
        return nowTime >= itemBeginTime && nowTime <= itemEndTime;
    }

    /**
     * 判断指定时间段是否包含在某个时间段
     *
     * @return
     */
    public static boolean isContainTime(VideoInfo timeSlot, long itemBeginTime, long itemEndTime) {
        return isInnerVideoArea(timeSlot, itemBeginTime) && isInnerVideoArea(timeSlot, itemEndTime);
    }

    /**
     * 判断时间是否在视频区域内
     *
     * @param timeSlot
     * @param nowTime
     * @return
     */
    public static boolean isInnerVideoArea(VideoInfo timeSlot, long nowTime) {
        return nowTime >= timeSlot.getStartTimeMillis() && nowTime <= timeSlot.getEndTimeMillis();
    }

    //=============绘制视频区域，判断时间End====================

}
